package gnss_plugs

import (
	"dgo/goutils/gnss"
	"time"
)

func RtcmBuf2ObsFile(rtcmBuf []byte, obsfile string, startT time.Time, onDecodeErrFunc func(buf []byte, err error)) (firstT time.Time, err error) {
	rtcmRaw := gnss.NewRtcm32RawBuf("")
	rnxW := NewGnssRinexObs3xWriter()
	rnxW.ConfigUseTempFile(false)
	rnxW.Filefmt = obsfile

	n := 0
	for i := 0; i < len(rtcmBuf); i++ {
		r := rtcmRaw.InputBuf(rtcmBuf[i])
		if r == 1 {
			msm, _ := gnss.DecodeRtcmMsm(rtcmRaw.GetRawBuf(), 24)
			if msm != nil {
				rec := gnss.NewGnssRtcmData(int(msm.Header.MsgId), gnss.TYPE_OBS, msm)
				rec.RtcmTime = msm.Header.DecodeTime(startT)
				startT = rec.RtcmTime
				if n == 0 {
					firstT = rec.RtcmTime
				}
				rnxW.OnRecvGnssDataRec(nil, rec)
				n++
			}
			rtcmRaw.Reset()
		}
	}
	rnxW.Stop()
	return
}
